"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.ServerConfig = void 0;
const Logger_1 = require("../engine/utils/Logger");
const RandomUtil_1 = require("../engine/utils/RandomUtil");
const Config_1 = require("./Config");
const path = require("path");
const fs = require("fs");
class ServerConfig {
    constructor() {
        this.serverType = "";
        this.id = 0;
        this.internalIp = "";
        this.http_port = 0;
        this.socket_port = 0;
        this.status = 1;
        this.nowPort = 0;
    }
    static readConfigFile() {
        return __awaiter(this, void 0, void 0, function* () {
            let configPath = path.resolve() + "/config/";
            if (Config_1.Config.environment == Config_1.Config.EnvironmentType.Dev) {
                configPath = configPath + "dev.json";
            }
            else if (Config_1.Config.environment == Config_1.Config.EnvironmentType.Test) {
                configPath = configPath + "test.json";
            }
            else if (Config_1.Config.environment == Config_1.Config.EnvironmentType.Online) {
                configPath = configPath + "online.json";
            }
            // else if (Config.environment == Config.EnvironmentType.yueyouhui) {
            //     configPath = configPath + "yueyouhui.json"
            // }
            // else if (Config.environment == Config.EnvironmentType.xj) {
            //     configPath = configPath + "xj.json"
            // }
            // else if (Config.environment == Config.EnvironmentType.chaoju_new) {
            //     configPath = configPath + "chaojunew.json"
            // }
            // else if (Config.environment == Config.EnvironmentType.Jk) {
            //     configPath = configPath + "jk.json"
            // }
            // else if(Config.environment == Config.EnvironmentType.chaoyou){
            //     configPath = configPath + "chaoyou.json"
            // }
            else {
                Logger_1.Logger.warn("不存在的环境配置文件", configPath);
            }
            // Logger.log("readConfigFile==", configPath)
            let data = yield fs.readFileSync(configPath);
            // Logger.log("readConfigFile=data=", data.toString())
            let configObj = JSON.parse(data.toString());
            return configObj;
        });
    }
    static initOneServerType(configObj, nowPort) {
        let config = new ServerConfig();
        config.ip = configObj["ip"];
        config.id = configObj["id"];
        config.internalIp = configObj["internalIp"];
        config.http_port = configObj["http_port"];
        config.socket_port = configObj["socket_port"];
        config.status = configObj["status"];
        if (nowPort == config.http_port || nowPort == config.socket_port) {
            ServerConfig.nowServerConfig = config;
            ServerConfig.nowServerConfig.nowPort = nowPort;
        }
        return config;
    }
    static initConfig(nowPort) {
        return __awaiter(this, void 0, void 0, function* () {
            let configObj = yield this.readConfigFile();
            let serverList = configObj[Config_1.Config.ServerType_List.account_server];
            this.account_server_List = [];
            for (let i = 0; i < serverList.length; i++) {
                this.account_server_List[i] = this.initOneServerType(serverList[i], nowPort);
                this.account_server_List[i].serverType = Config_1.Config.ServerType_List.account_server;
            }
            serverList = configObj[Config_1.Config.ServerType_List.WebServer];
            this.WebServer_List = [];
            for (let i = 0; i < serverList.length; i++) {
                this.WebServer_List[i] = this.initOneServerType(serverList[i], nowPort);
                this.WebServer_List[i].serverType = Config_1.Config.ServerType_List.WebServer;
            }
            serverList = configObj[Config_1.Config.ServerType_List.gm_server];
            this.gmServer_List = [];
            for (let i = 0; i < serverList.length; i++) {
                this.gmServer_List[i] = this.initOneServerType(serverList[i], nowPort);
                this.gmServer_List[i].serverType = Config_1.Config.ServerType_List.gm_server;
            }
        });
    }
    static randomAccountServer() {
        return RandomUtil_1.default.randomItem(this.account_server_List);
    }
    static randomHallServer() {
        return RandomUtil_1.default.randomItem(this.hall_server_List);
    }
    getHttpUrl() {
        return "http://" + this.ip + ":" + this.http_port;
    }
    getInternalUrl() {
        return "http://" + this.internalIp + ":" + this.http_port;
    }
    getInternalWsUrl() {
        let url = "ws://" + this.internalIp + ":" + this.socket_port;
        return url;
    }
    static getAllServer() {
        let result = [];
        result = result.concat(this.WebServer_List);
        result = result.concat(this.account_server_List);
        result = result.concat(this.hall_server_List);
        return result;
    }
}
exports.ServerConfig = ServerConfig;
ServerConfig.Status_Type = {
    Normal: 1,
    Weihu: 999,
};
ServerConfig.account_server_List = [];
ServerConfig.WebServer_List = [];
ServerConfig.gmServer_List = [];
ServerConfig.hall_server_List = [];
